import 'package:flutter/material.dart';

class CanvasDemo extends StatefulWidget {
  const CanvasDemo({super.key});

  @override
  State<CanvasDemo> createState() => _CanvasDemoState();
}

class _CanvasDemoState extends State<CanvasDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('标题'),
      ),
      body: Column(
        children: [
          Container(
            width: 300,
            height: 300,
            child: CustomPaint(
              painter: CanvasPaint(),
            ),
          )
        ],
      ),
    );
  }
}

class CanvasPaint extends CustomPainter {
  final Paint _paint = Paint()..color = Colors.black;

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    Path _path = Path();
    _path
      ..moveTo(200, 0)
      ..lineTo(250, 70)
      ..lineTo(250, 150)
      ..lineTo(200, 80);
    _path.close();
    canvas.drawPath(_path, _paint);
    _paint
      ..style = PaintingStyle.stroke
      ..color = Colors.red
      ..strokeWidth = 2;
    _path.reset();
    _path
      ..moveTo(200, 0)
      ..lineTo(150, 70)
      ..lineTo(150, 150)
      ..lineTo(200, 80)
      ..close();
    canvas.drawPath(_path, _paint);
    _path.reset();

    // _paint
    //   ..color = Colors.green
    //   ..style = PaintingStyle.fill;
    // _path
    //   ..relativeMoveTo(0, 0)
    //   ..relativeLineTo(100, 120)
    //   ..relativeLineTo(-10, -60)
    //   ..relativeLineTo(
    //     60,
    //     -10,
    //   )
    //   ..close();
    // canvas.drawPath(_path, _paint);

    _path.reset();
    _paint
      ..style = PaintingStyle.stroke
      ..color = Colors.green
      ..strokeWidth = 2;
    _path
      ..relativeMoveTo(0, 0)
      ..relativeLineTo(100, 120)
      ..relativeLineTo(-10, -60)
      ..relativeLineTo(
        60,
        -10,
      )
      ..close();
    canvas.drawPath(_path, _paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}
